home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Personal Computer World 2009 February
/
PCWFEB09.iso
/
Software
/
FromTheMag
/
JW FLV MEDIA PLAYER 4.2
/
mediaplayer.exe
/
player.swf
/
scripts
/
com
/
jeroenwijering
/
models
/
HTTPModel.as
< prev
next >
Wrap
Text File
|
2008-11-04
|
11KB
|
338 lines
package com.jeroenwijering.models
{
import com.jeroenwijering.events.ModelEvent;
import com.jeroenwijering.events.ModelStates;
import com.jeroenwijering.player.Model;
import com.jeroenwijering.utils.NetClient;
import flash.events.AsyncErrorEvent;
import flash.events.ErrorEvent;
import flash.events.IOErrorEvent;
import flash.events.NetStatusEvent;
import flash.events.SecurityErrorEvent;
import flash.media.SoundTransform;
import flash.media.Video;
import flash.net.NetConnection;
import flash.net.NetStream;
import flash.utils.clearInterval;
import flash.utils.setInterval;
public class HTTPModel implements ModelInterface
{
private var stream:NetStream;
private var keyframes:Object;
private var h264:Boolean;
private var loaded:Number;
private var offset:Number;
private var timeinterval:Number;
private var connection:NetConnection;
private var timeoffset:Number;
private var model:Model;
private var loadinterval:Number;
private var transform:SoundTransform;
private var video:Video;
public function HTTPModel(param1:Model)
{
super();
model = param1;
connection = new NetConnection();
connection.addEventListener(NetStatusEvent.NET_STATUS,statusHandler);
connection.addEventListener(SecurityErrorEvent.SECURITY_ERROR,errorHandler);
connection.addEventListener(AsyncErrorEvent.ASYNC_ERROR,errorHandler);
connection.connect(null);
stream = new NetStream(connection);
stream.addEventListener(NetStatusEvent.NET_STATUS,statusHandler);
stream.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
stream.addEventListener(AsyncErrorEvent.ASYNC_ERROR,metaHandler);
stream.bufferTime = model.config["bufferlength"];
stream.client = new NetClient(this);
video = new Video(320,240);
video.attachNetStream(stream);
transform = new SoundTransform();
stream.soundTransform = transform;
if(model.config["mute"] == true)
{
volume(0);
}
else
{
volume(model.config["volume"]);
}
quality(model.config["quality"]);
offset = timeoffset = 0;
}
public function stop() : void
{
clearInterval(loadinterval);
clearInterval(timeinterval);
offset = timeoffset = 0;
h264 = false;
keyframes = undefined;
if(stream.bytesLoaded != stream.bytesTotal)
{
stream.close();
}
stream.pause();
}
private function getToken() : String
{
return model.config["token"];
}
private function loadHandler() : void
{
var _loc1_:* = undefined;
loaded = stream.bytesLoaded;
_loc1_ = stream.bytesTotal;
if(loaded >= _loc1_ && loaded > 0)
{
clearInterval(loadinterval);
}
model.sendEvent(ModelEvent.LOADED,{
"loaded":loaded,
"total":_loc1_ + offset,
"offset":offset
});
}
public function quality(param1:Boolean) : void
{
if(param1 == true)
{
video.smoothing = true;
video.deblocking = 3;
}
else
{
video.smoothing = false;
video.deblocking = 1;
}
}
private function getOffset(param1:Number, param2:Boolean = false) : Number
{
var _loc3_:* = undefined;
_loc3_ = 0;
while(_loc3_ < keyframes.times.length)
{
if(keyframes.times[_loc3_] <= param1 && (keyframes.times[_loc3_ + 1] >= param1 || !keyframes.times[_loc3_ + 1]))
{
if(param2 == true)
{
return keyframes.times[_loc3_];
}
return keyframes.filepositions[_loc3_];
}
_loc3_++;
}
return 0;
}
private function metaHandler(param1:ErrorEvent) : void
{
model.sendEvent(ModelEvent.META,{"error":param1.text});
}
private function timeHandler() : void
{
var _loc1_:* = undefined;
var _loc2_:* = undefined;
var _loc3_:* = undefined;
_loc1_ = Math.round(stream.bufferLength / stream.bufferTime * 100);
_loc2_ = Math.round(stream.time * 10) / 10;
if(h264)
{
_loc2_ += timeoffset;
}
_loc3_ = model.playlist[model.config["item"]]["duration"];
if(_loc1_ < 100 && _loc2_ < Math.abs(_loc3_ - stream.bufferTime * 2))
{
model.sendEvent(ModelEvent.BUFFER,{"percentage":_loc1_});
if(model.config["state"] != ModelStates.BUFFERING && _loc1_ < 10)
{
model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.BUFFERING});
}
}
else if(model.config["state"] == ModelStates.BUFFERING)
{
model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PLAYING});
}
if(_loc3_ > 0)
{
model.sendEvent(ModelEvent.TIME,{
"position":_loc2_,
"duration":_loc3_
});
}
}
private function statusHandler(param1:NetStatusEvent) : void
{
if(param1.info.code == "NetStream.Play.Stop")
{
if(model.config["state"] != ModelStates.COMPLETED)
{
clearInterval(timeinterval);
model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.COMPLETED});
}
}
else if(param1.info.code == "NetStream.Play.StreamNotFound")
{
stop();
model.sendEvent(ModelEvent.ERROR,{"message":"Video stream not found: " + model.playlist[model.config["item"]]["file"]});
}
else
{
model.sendEvent(ModelEvent.META,{"info":param1.info.code});
}
}
private function errorHandler(param1:ErrorEvent) : void
{
model.sendEvent(ModelEvent.ERROR,{"message":param1.text});
}
public function load() : void
{
var _loc1_:* = undefined;
model.mediaHandler(video);
if(stream.bytesLoaded != stream.bytesTotal)
{
stream.close();
}
_loc1_ = model.playlist[model.config["item"]]["file"];
if(model.config["streamer"] == "lighttpd")
{
if(h264)
{
_loc1_ += "?start=" + timeoffset;
}
else
{
_loc1_ += "?start=" + offset;
}
}
else if(model.config["streamer"].indexOf("?") > -1)
{
_loc1_ = model.config["streamer"] + "&file=" + _loc1_ + "&start=" + offset;
}
else
{
_loc1_ = model.config["streamer"] + "?file=" + _loc1_ + "&start=" + offset;
}
_loc1_ += "&id=" + model.config["id"];
_loc1_ += "&client=" + encodeURI(model.config["client"]);
_loc1_ += "&version=" + encodeURI(model.config["version"]);
_loc1_ += "&width=" + model.config["width"];
if(getToken())
{
_loc1_ += "&token=" + getToken();
}
stream.play(_loc1_);
clearInterval(loadinterval);
clearInterval(timeinterval);
loadinterval = setInterval(loadHandler,100);
timeinterval = setInterval(timeHandler,100);
model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.BUFFERING});
}
public function onData(param1:Object) : void
{
var _loc2_:* = undefined;
if(param1.type == "metadata" && !h264)
{
if(param1.width)
{
video.width = param1.width;
video.height = param1.height;
}
if(param1.seekpoints)
{
h264 = true;
keyframes = new Object();
keyframes.times = new Array();
keyframes.filepositions = new Array();
for(_loc2_ in param1.seekpoints)
{
keyframes.times[_loc2_] = Number(param1.seekpoints[_loc2_]["time"]);
keyframes.filepositions[_loc2_] = Number(param1.seekpoints[_loc2_]["offset"]);
}
}
else if(param1.keyframes)
{
keyframes = param1.keyframes;
}
if(model.playlist[model.config["item"]]["start"] > 0)
{
seek(model.playlist[model.config["item"]]["start"]);
}
model.sendEvent(ModelEvent.META,param1);
}
else if(param1.type != "metadata")
{
model.sendEvent(ModelEvent.META,param1);
}
}
public function volume(param1:Number) : void
{
transform.volume = param1 / 100;
stream.soundTransform = transform;
}
public function play() : void
{
stream.resume();
timeinterval = setInterval(timeHandler,100);
model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PLAYING});
}
public function seek(param1:Number) : void
{
var _loc2_:* = undefined;
clearInterval(timeinterval);
_loc2_ = getOffset(param1);
if(_loc2_ < offset || _loc2_ > offset + loaded)
{
offset = _loc2_;
timeoffset = getOffset(param1,true);
load();
}
else
{
if(h264)
{
stream.seek(param1 - timeoffset);
}
else
{
stream.seek(param1);
}
play();
}
}
public function pause() : void
{
clearInterval(timeinterval);
stream.pause();
model.sendEvent(ModelEvent.STATE,{"newstate":ModelStates.PAUSED});
}
}
}